#include "MyString.h"
#include <cstring>

MyString::MyString() : size(0), capacity(15)
{
    string = new char[capacity];
    memset(string,0,capacity);
}

MyString::MyString(const char *str)
{
    size = strlen(str);
    capacity = size + 1;
    string = new char[capacity];
    strcpy(string, str);
}

MyString::MyString(const MyString &s):size(s.size),capacity(s.capacity)
{
    string = new char[capacity];
    strcpy(string,s.string);
}

MyString& MyString::operator=(const MyString &s)
{
    delete []string; //构造后进行赋值，所以可能已经有内存了，需要进行释放
    size = s.size;
    capacity = s.capacity;
    string = new char[capacity];
    strcpy(string,s.string);
    return *this;
}

MyString &MyString::operator+=(const MyString &s)
{
    if(s.string == nullptr || s.size == 0)
        return *this;
    size += s.size;  // 新长度
    if(size + 1 > capacity)
    {
        capacity = size + 1;
        char *newS = new char[capacity];
        //清空一下内存数据
        memset(newS,0,capacity);
        if(string)
            strcpy(newS,string);
        delete[]string;
        string = newS;
    }
    strcat(string,s.string);
    return *this;
}

MyString &MyString::operator+=(const char &c)
{
    if(size + 1 >= capacity)
    {
        capacity *= 2;
        char *newS = new char[capacity];
        //清空一下内存数据
        memset(newS,0,capacity);
        strcpy(newS,string);
        delete[]string;
        string = newS;
    }
    string[size++] = c;
    string[size] = '\0';
    return *this;
}

MyString MyString::operator+(const MyString &s)
{
    MyString result = *this;
    result += s;
    return result;
}

MyString &MyString::operator-=(const MyString &s)
{
    char *dest = strstr(this->string,s.string);
    while(dest)
    {
        char *back = dest + s.size;
        while(*back != '\0')
        {
            *dest++ = *back++;
        }
        *dest ='\0';
        //删完一个继续寻找下一个
        dest = strstr(this->string,s.string);
    }
    return *this;
}

MyString MyString::operator-(const MyString &s)
{
    MyString result = *this;
    result -= s;
    return result;
}

char &MyString::operator[](int index)
{
    return string[index];
}

bool MyString::operator==(const MyString &s)
{
    return strcmp(string,s.string) == 0;
}

MyString::operator int()
{
    return atoi(string);
}

MyString::operator double()
{
    return atof(string);
}

MyStringArray MyString::split(const MyString &delim)
{
    if(delim.string == nullptr || delim.size == 0)
        return MyStringArray();
    MyString temp = *this;
    MyStringArray result;
    char* dest = strtok(temp.string,delim.string);
    while(dest)
    {
        result += dest;
        dest = strtok(nullptr,delim.string);
    }
    return result;
}

void MyString::clear()
{
    memset(string,0,capacity); //先清空字符串
    size = 0;
}

MyString::~MyString()
{
        delete []string;
}

std::ostream& operator<<(std::ostream& os,const MyString& s)
{
    std::cout<<s.string;
    return os;
}

std::istream &operator>>(std::istream &is, MyString &s)
{
    s.clear();
    char c = 0;
    while((c = getchar()) != '\n')             //getchar(); //从终端读取一个字符
    {
        s += c;
    }
    return is;
}


MyStringArray::MyStringArray():size(0),capacity(15)
{
    array = new MyString[capacity];
}

MyStringArray::MyStringArray(const MyStringArray &s)
{
    capacity = s.capacity;
    size = s.size;
    array = new MyString[capacity];
    for(int i = 0;i<size;i++)
    {
        array[i] = s.array[i];
    }
}

MyStringArray::~MyStringArray()
{
    delete[] array; // 释放动态分配的数组
    array = nullptr; // 将指针置为 nullptr，防止悬空指针
}

MyStringArray& MyStringArray::operator+=(const MyString &str)
{
    if(size >= capacity)
    {
        capacity *= 2;
        MyString *newA = new MyString[capacity];
        for(int i=0;i<size;i++)
        {
            newA[i] = array[i];
        }
        if(array)
            delete []array;
        array = newA;
    }
    array[size++] = str;
    return *this;
}


MyStringArray MyStringArray::operator+(const MyString &str)
{
    MyStringArray result = *this;
    result += str;
    return result;
}


void MyStringArray::show()
{
    for(int i = 0;i<size;i++)
    {
        std::cout << array[i]<<std::endl;
    }
}


MyStringArray& MyStringArray::operator=(const MyStringArray& s)
{
    if (this == &s) // 防止自赋值
        return *this;

    delete[] array; // 释放旧内存

    size = s.size;
    capacity = s.capacity;
    array = new MyString[capacity];
    for (int i = 0; i < size; i++)
    {
        array[i] = s.array[i]; // 调用 MyString 的赋值运算符
    }
    return *this;
}